package com.finger.gulimall.member.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.finger.gulimall.member.vo.AddrFareVo;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.finger.gulimall.member.entity.MemberReceiveAddressEntity;
import com.finger.gulimall.member.service.MemberReceiveAddressService;
import com.finger.common.utils.PageUtils;
import com.finger.common.utils.R;


/**
 * 会员收货地址
 *
 * @author finger
 * @email 909128695@qq.com
 * @date 2022-03-16 18:12:38
 */
@RestController
@RequestMapping("member/memberreceiveaddress")
public class MemberReceiveAddressController {
    @Autowired
    private MemberReceiveAddressService memberReceiveAddressService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("member:memberreceiveaddress:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberReceiveAddressService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 根据会员id查找用户地址
     * @param id 用户id
     * @return
     */
    @RequestMapping("/findById/{id}")
    public R findAddrsById(@PathVariable Long id){
        QueryWrapper<MemberReceiveAddressEntity> addressEntityQueryWrapper = new QueryWrapper<MemberReceiveAddressEntity>().eq("member_id", id);
        List<MemberReceiveAddressEntity> addressEntities = memberReceiveAddressService.list(addressEntityQueryWrapper);
        return R.ok().setData(addressEntities);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    // @RequiresPermissions("member:memberreceiveaddress:info")
    public R info(@PathVariable("id") Long id) {
        MemberReceiveAddressEntity memberReceiveAddress = memberReceiveAddressService.getById(id);

        return R.ok().put("memberReceiveAddress", memberReceiveAddress);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("member:memberreceiveaddress:save")
    public R save(@RequestBody MemberReceiveAddressEntity memberReceiveAddress) {
        memberReceiveAddressService.save(memberReceiveAddress);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("member:memberreceiveaddress:update")
    public R update(@RequestBody MemberReceiveAddressEntity memberReceiveAddress) {
        memberReceiveAddressService.updateById(memberReceiveAddress);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("member:memberreceiveaddress:delete")
    public R delete(@RequestBody Long[] ids) {
        memberReceiveAddressService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @GetMapping("/addrFareForOrder")
    public R addrFare(@RequestParam("addrId")Long addrId){

        AddrFareVo fare = memberReceiveAddressService.addrFare(addrId);

        return R.ok().setData(fare);
    }
}
